package com.atguigu.paymentdemo.task;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.RefundInfo;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.service.RefundInfoService;
import com.atguigu.paymentdemo.service.WxPayService;
import com.atguigu.paymentdemo.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author longge
 * @Title:
 * @Description:
 * @date 2023/2/6 23:32
 */
@Slf4j
@Component
public class WxPayTask {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private RefundInfoService refundInfoService;

    @Autowired
    private WxPayService wxPayService;

    /**
     *     秒 分 时 日 月 周
     *     ？：不指定
     *     *:秒都执行
     *     1-3：从第1秒开始，第3秒结束
     *     0/3：从第0秒开始，每隔3秒执行一次
     *     1,2,3：在指定的第1，2，3秒执行一次
     * @Description: 查询超过5分钟未支付的订单
     */
//    @Scheduled(cron = "0/30 * * * * ?")
    @Scheduled(cron = "0 * * * 5/1 ?")
    public void orderConfirm() throws IOException {
        log.info("orderConfirm 执行...");
        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5);
        for (OrderInfo orderInfo : orderInfoList) {
            log.info("超时订单 ===》 {}",orderInfo.getOrderNo());
            //调用微信接口，核实订单
            wxPayService.checkOrderStatus(orderInfo.getOrderNo());
        }
    }

    /**
     * @Description: 查询退款
     * @throws IOException
     */
//    @Scheduled(cron = "15/30 * * * * ?")
    @Scheduled(cron = "0 * * * 5/1 ?")
    public void refundConfirm() throws IOException {
        log.info("refundConfirm 执行...");
        List<RefundInfo> refundInfoList = refundInfoService.getProcessingRedundInfoByDuration(5);
        for (RefundInfo refundInfo : refundInfoList) {
            log.info("超时退款核查 ===> {}",refundInfo);
            wxPayService.checkRefundOrder(refundInfo.getRefundNo());
        }
    }
}
